<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c-rt"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ tag pageEncoding="UTF-8"%>
<%@ attribute name="imgDomain" required="true"%>
<%@ attribute name="prodInvMap" required="true" type="java.util.Map"%>
<%@ attribute name="product" required="true" type="com.yaodian100.ec.product.entity.Product"%>
$(function(){
	<c-rt:forEach items="${prodInvMap}" var="entry">
	$(document.body).data('inv_${entry.key}','${entry.value.availableInventory + entry.value.cancelInventory - entry.value.soldInventory}');
	</c-rt:forEach>
	<c-rt:forEach items="${product.specMajors}" var="specMajor" varStatus="status">
	$("div.select-area").data('${specMajor.mainSpecId}',${specMajor.jsonDetail});

	<c-rt:choose>
    <c-rt:when test="${product.graphicType eq 'R'}">
    $("div.select-area").data('img_'+'${specMajor.mainSpecId}','${specMajor.imgRmain}');
	$("div.select-area").data('simg_'+'${specMajor.mainSpecId}','${specMajor.imgRmspec}');
    </c-rt:when>
    <c-rt:otherwise>
    $("div.select-area").data('img_'+'${specMajor.mainSpecId}','${specMajor.imgMain}');
	$("div.select-area").data('simg_'+'${specMajor.mainSpecId}','${specMajor.imgMspec}');
    </c-rt:otherwise></c-rt:choose>
	</c-rt:forEach>
	$(":a[id*=ms_]").each(function(){
		var msId = $(this).attr('id').split('_')[1];
		var secSpec = $("div.select-area").data(msId);
		var haseQty = false;
		$(secSpec).each(function(index, temp) {
			if(parseInt($(document.body).data('inv_'+temp.specId),10)>0){
				haseQty = true;
			}
		});
		if(!haseQty){
			$('<span class="pd-type-none" title="'+$(this).text()+'-售完">'+$(this).html()+'</span>').insertAfter($(this));
			$(this).remove();
		}
	});
	//default image
	if($("#cover_id").length > 0){
		setImgUrl($("div.select-area").data('img_'+ $("#cover_id").val()));
	}else{
		setImgUrl($("div.select-area").data('img_${product.specMajors[0].mainSpecId}'));
	}

	//default select
	if($("#cover_id").length > 0 && $("#ms_"+$("#cover_id").val()).length > 0){
		$("#ms_"+$("#cover_id").val()).click();
	}else if($(":a[onclick*=selectProdMainSpec]").length>0){
		$(":a[onclick*=selectProdMainSpec]").eq(0).click();
	}
	
	var selectedMs = "<%=request.getParameter("mId") %>";
	var selectedSs = "<%=request.getParameter("sId") %>";
	if( selectedMs!="null" && selectedMs!="请选择"){
		$("#ms_"+selectedMs).click();
	}
	if( selectedSs!="null" && selectedSs!="请选择"){
		$("#ss_"+selectedSs).click();
	}
});

function setImgUrl(imgUrls){
	imgUrl = imgUrls.split(',');
}
function changeImgMain(index){
	if(imgUrl.length > index){
		$(".pimage > img").eq(0).attr('src','${imgDomain}'+imgUrl[index]);
		$(".img-list > a").removeAttr('class');
		$(".img-list > a").eq(index).attr('class','list-fo');
	}
}
function selectProdMainSpec(msId){
	if($("#ms_"+msId).attr('class')!='pd-type-fo'){
		var secSpec = $("div.select-area").data(msId);
		var secSpecHtml = '';
		$(secSpec).each(function(index, temp) {
			if(parseInt($(document.body).data('inv_'+temp.specId),10)>0){
				secSpecHtml += '<a class="pd-type" href="javascript:;" id="ss_'+ temp.specId +'" onclick="selectProdSecondSpec(' + temp.specId + ');" title="'+temp.specName+'">'+temp.specName+'</a>';
			}else{
				secSpecHtml += '<span class="pd-type-none" title="'+temp.specName+'-售完">'+temp.specName+'-售完</span>';
			}
		});
		$("#secSpecArea").html(secSpecHtml);
		$(":a[id*=ms_].pd-type-fo").attr('class','pd-type');
		$("#ms_"+msId).attr('class','pd-type-fo');
		$("#msem1").html($("#ms_"+msId).attr('title'));
		$("#msem2").html('"<em>'+$("#ms_"+msId).attr('title')+'</em>"');
		$("#ssem1").html('');
		$("#ssem2").html('');
		if($(":a[id*=ss_]").length>0){
			$(":a[id*=ss_]").eq(0).click();
		}
		// change img
		$("div.img-list > a > img").hide();	
		setImgUrl($("div.select-area").data('img_'+ msId));
		var simg = $("div.select-area").data('simg_'+ msId).split(',');
		for(var i = 0 ; i < simg.length ; i++){
			$("div.img-list > a > img").eq(i).attr('src','${imgDomain}'+simg[i]);
			$("div.img-list > a > img").eq(i).show();
		}
		changeImgMain(0);
		$('#mspec').val(msId);
	}
}

function selectProdSecondSpec(ssId){
	if($("#ss_"+ssId).attr('class')!='pd-type-fo'){
		$(":a[id*=ss_]").attr('class','pd-type');
		$("#ss_"+ssId).attr('class','pd-type-fo');
		$(":em[id*=ssem]").html();
		$("#ssem1").html($("#ss_"+ssId).attr('title'));
		$("#ssem2").html(',"<em>'+$("#ss_"+ssId).attr('title')+'</em>"');
		$('#sspec').val(ssId);
	}
}